package com.lmk.order.service.impl;

import com.lmk.basic.service.Impl.BaseServiceImpl;
import com.lmk.basic.util.LoginContext;
import com.lmk.basic.util.PageList;
import com.lmk.order.domain.OrderPetAcquisition;
import com.lmk.order.dto.OrderDto;
import com.lmk.order.mapper.OrderPetAcquisitionMapper;
import com.lmk.order.query.OrderPetAcquisitionQuery;
import com.lmk.order.service.IOrderPetAcquisitionService;
import com.lmk.org.domain.Employee;
import com.lmk.org.domain.Shop;
import com.lmk.org.mapper.EmployeeMapper;
import com.lmk.org.mapper.ShopMapper;
import com.lmk.pet.domain.Pet;
import com.lmk.pet.domain.SearchMasterMsg;
import com.lmk.pet.mapper.PetMapper;
import com.lmk.pet.mapper.SearchMasterMsgMapper;
import com.lmk.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        //获取登录信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //获取店铺id
        Long shop_id = employee.getShop_id();
        //店铺管理员 = 店长 or 店员
        if(shop_id!=null){
            //将店铺id设置到query
            query.setShopId(shop_id);
            //获取店铺的对象
            Shop shop = shopMapper.loadById(shop_id);
            //shop.getAdminId() = employee_id
            System.out.println(shop.getAdminId().equals(employee.getId()));
            //如果店铺管理员id 不等于 当前登录人的id
            if(shop.getAdminId() != employee.getId()){//店员
                //店员只能查询自己处理的寻主消息
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }



    @Override
    public void del(Long id) {
        //通过id查询OrderPetAcquisition对象
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        Long searchMasterMsgId = orderPetAcquisition.getSearchMasterMsgId();
        SearchMasterMsg oneId = searchMasterMsgMapper.loadById(searchMasterMsgId);
        //把店铺id置为null
        oneId.setShopId(null);
        //修改对象
        searchMasterMsgMapper.update(oneId);
        //2.删除宠物信息
        petMapper.remove(oneId.getPetType());
        // 删除订单
        super.del(id);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        // 获取前端传入信息
        Long orderId  = orderDto.getId();
        Integer paytype = orderDto.getPaytype();
        BigDecimal money = orderDto.getMoney();
        // 修改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(orderId);
        // 状态设置为1,已处理
        orderPetAcquisition.setState(1);
        // 设置金额
        orderPetAcquisition.setPrice(money);
        // 设置支付方式
        orderPetAcquisition.setPaytype(paytype);
        // 保存到订单表中
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 修改寻主消息
        Long searchMasterMsgId = orderPetAcquisition.getSearchMasterMsgId();
        // 根据id找到我们需要修改的消息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(searchMasterMsgId);
        // 状态啊设置为2
        searchMasterMsg.setState(2);
        // 重新保存
        searchMasterMsgMapper.update(searchMasterMsg);

        //重新设置金额
        Long petId = orderPetAcquisition.getPetId();
        Pet pet = petMapper.loadById(petId);
        pet.setCostprice(money);
        petMapper.update(pet);

    }

    /*mq修改*/
    @Override
    public void remove(Long valueOf) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(valueOf);
        searchMasterMsgMapper.remove(orderPetAcquisition.getSearchMasterMsgId());
        //2.删除宠物信息
        petMapper.remove(orderPetAcquisition.getPetId());
        //3.改状态
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

    }
}
